const Router = require('koa-router')
const router = new Router();
const { returnMsg, queryFn, jwtVerify } = require("../../config")
const moment = require('moment')

// 文章列表
router.post("/list", async ctx => {
    let token = ctx.request.headers['cms-token']
    if(!jwtVerify(token)){
        ctx.body = returnMsg(2, '查询用户信息失败', "token过期或者该用户不存在")
        return
    } 
    let sql = `select count(*) rows from article`
    // let sql1 = `select * from article LIMIT ${index}, ${counts}`
    // let sql = `select id, title,subTitle,date from  article`
    let result = await queryFn(sql)
    let total = result[0].rows

    let {current, counts} = ctx.request.body
    if(!current || !counts){
        ctx.body = returnMsg(1,"参数错误")
        return
    }
    let sql1 = `select id, title,subTitle,date from article LIMIT ${current ===0 ? 0 : (current-1)*counts}, ${counts}`
    let result1 = await queryFn(sql1)

    ctx.body = returnMsg(0,"分页查询成功",{
        total,
        current,
        counts,
        arr:result1
    })
})

// 根据前端过来的id获取文章
router.post("/list/id",async ctx =>{
    let token = ctx.request.headers['cms-token']
    if(!jwtVerify(token)){
        ctx.body = returnMsg(2, '查询用户信息失败', "token过期或者该用户不存在")
        return
    }  
    let {id} = ctx.request.body
    let sql = `select * from article where id = '${id}'`
    let result = await queryFn(sql)
    if(result.length>0){
        ctx.body = returnMsg(0,'获取成功',result[0])
    }else{
        ctx.body = returnMsg(1,'该文章不存在')
    }
    
})

// 文章编辑
router.post('/list/edit', async ctx => {
    let token = ctx.request.headers['cms-token']
    if(!jwtVerify(token)){
        ctx.body = returnMsg(2, '查询用户信息失败', "token过期或者该用户不存在")
        return
    }   
    let sql2 = `select editable,username from user where token = '${token}'`
    let result2 = await queryFn(sql2)
    if(result2[0].editable == 1){
        let {id,title,subTitle,content} = ctx.request.body; 
        if(!id || !title || !content){
            ctx.body = returnMsg(1,'参数错误')
            return
        }
        let sql = `select * from article where id = '${id}'`
        let result =await queryFn(sql)
        if(result.length>0){
            let mydate = moment().format('YYYY-MM-DD hh:mm:ss')
            let sql1 = `update article set title = '${title || ''}', subTitle = '${subTitle || ''}', content = '${content || ''}', date='${mydate}',author='${result2[0].username}' where id =${id}`
            let reslult1 = await queryFn(sql1)
            sql3 = `select id, title,subTitle,date from  article`
            let result3 = await queryFn(sql)
            ctx.body = returnMsg(0,'文章修改成功',result3)
        }else{
            ctx.body = returnMsg(1,'当前编辑的文章不存在')
            return
        }
    }else{
        ctx.body = returnMsg(1,'该用户没有编辑权限')
        return
    }
})

// 文章删除
router.post('/list/delelt',async ctx =>{
    let token = ctx.request.headers['cms-token']
    if(!jwtVerify(token)){
        ctx.body = returnMsg(2, '查询用户信息失败', "token过期或者该用户不存在")
        return
    }  
    let {id} = ctx.request.body
    console.log(111)
    if(!id){
        ctx.body = returnMsg(1, '参数错误')
        return
    }
    // 判断用户是否有删除文章的权限
    let sql3 = `select editable from  user where token = '${token}'`
    let result3 = await queryFn(sql3)
    if(result3[0].editable === 0){
        ctx.body = returnMsg(1, '该用户无删除权限')
        return
    }
    console.log(result3)
    let sql = `select * from  article where id = '${id}'`
    let result = await queryFn(sql)
    if(result.length == 0){
        ctx.body = returnMsg(2, '删除的文章不存在')
        return
    }
    let sql1 = `delete from article where id = '${id}'`
    let result1 = await queryFn(sql1)
    ctx.body = returnMsg(0,"删除成功")
})

router.post('/list/AddPage',async ctx =>{
    for (let index = 0; index < 200; index++) {
        let sql = `insert into article values (null,'你好${index}','世界${index}','张三${index}','${moment().format("YYYY-MM-DD hh:mm:ss")}','<p>我是应该风刷机 哈哈哈 你好世界hello world<p>')`
        await queryFn(sql)
    }
    ctx.body=123
})

// 文章分页
router.post('/list/page',async ctx =>{
    let index = 10;
    let counts = 10;
    let sql = `select count(*) rows from article`
    let sql1 = `select * from article LIMIT ${index}, ${counts}`
    let result = await queryFn(sql)
    ctx.body = returnMsg(2, '查询用户信息失败', "token过期或者该用户不存在")
    /* let token = ctx.request.headers['cms-token']
    if(!jwtVerify(token)){
        ctx.body = returnMsg(2, '查询用户信息失败', "token过期或者该用户不存在")
        return
    }  
    let {id} = ctx.request.body
    console.log(111)
    if(!id){
        ctx.body = returnMsg(1, '参数错误')
        return
    }
    // 判断用户是否有删除文章的权限
    let sql3 = `select editable from  user where token = '${token}'`
    let result3 = await queryFn(sql3)
    if(result3[0].editable === 0){
        ctx.body = returnMsg(1, '该用户无删除权限')
        return
    }
    console.log(result3)
    let sql = `select * from  article where id = '${id}'`
    let result = await queryFn(sql)
    if(result.length == 0){
        ctx.body = returnMsg(2, '删除的文章不存在')
        return
    }
    let sql1 = `delete from article where id = '${id}'`
    let result1 = await queryFn(sql1)
    ctx.body = returnMsg(0,"删除成功") */
})

// 文章添加
router.post('/list/add',async ctx =>{
    let token = ctx.request.headers['cms-token']
    if(!jwtVerify(token)){
        ctx.body = returnMsg(2, '查询用户信息失败', "token过期或者该用户不存在")
        return
    }   
    let sql2 = `select editable,username from user where token = '${token}'`
    let result2 = await queryFn(sql2)
    if(result2[0].editable == 1){
        let {title,subTitle,content} = ctx.request.body; 
        if( !title || !content){
            ctx.body = returnMsg(1,'参数错误')
            return
        }
        let mydate = moment().format('YYYY-MM-DD hh:mm:ss')
        let sql1 = `insert into article values (null,'${title || ''}','${subTitle || ''}' ,'${result2[0].username || ''}','${mydate || ''}','${content || ''}')`
        let reslult1 = await queryFn(sql1)
        // sql3 = `select id, title,subTitle,date from  article`
        // let result3 = await queryFn(sql)
        
        ctx.body = returnMsg(0,'文章添加成功')
    }else{
        ctx.body = returnMsg(1,'该用户没有编辑权限')
        return
    }
})

module.exports = router